package com.baiytfp.hf.HFDataExchange.action.controller.service;

import com.alibaba.fastjson.JSONObject;
import com.baiytfp.hf.HFDataExchange.common.model.table.Operate_record;
import com.baiytfp.hf.baiytfp.common.util.StringUtil;
import com.baiytfp.hf.baiytfp.framework.base.BaseController;
import com.baiytfp.hf.baiytfp.framework.model.state.Charset;
import com.baiytfp.hf.baiytfp.framework.model.vo.HFHttpResp;
import com.baiytfp.hf.baiytfp.hfutils.HFHttp;
import com.baiytfp.hf.baiytfp.hfutils.HFJson;
import com.baiytfp.hf.baiytfp.hfutils.HFRequest;
import com.baiytfp.hf.baiytfp.hfutils.HFResponse;
import com.baiytfp.hf.HFDataExchange.action.service.OperateRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/service/operaterecord/api")
public class OperateRecordApiSController extends BaseController {

	@Autowired
    private OperateRecordService operateRecordService;
	
	//新增一条记录
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public void add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String params = HFRequest.getIstance().convertString(request, Charset.UTF8);
		Operate_record record = HFJson.getInstance().parseObject(params, Operate_record.class);
		if(StringUtil.isNull(record.getRecord_table_id())
				|| StringUtil.isNull(record.getRecord_table_name())
				|| StringUtil.isNull(record.getRecord_type())
				|| StringUtil.isNull(record.getRecord_man())
				) {
			HFResponse.getIstance().returnResult(response, -1, "参数不完整", null);
			return;
		}
		long res = operateRecordService.add(record);
		if(res > 0) {
			HFResponse.getIstance().returnResult(response, 0, "操作成功", new JSONObject());
		} else {
			HFResponse.getIstance().returnResult(response, -2, "操作失败", null);
		}
	}

	@RequestMapping("test")
	public void test(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String url = "http://localhost:8080/HFDataExchange/service/operaterecord/api/add.do";
		Map params = new HashMap();

		params.put("record_table_name", "hfdataexchange_user");
		params.put("record_table_id", "admin");
		params.put("record_man", "admin");
		params.put("record_type", "1");


		HashMap<String, Object> headers = new HashMap<>();
		headers.put("Content-Type", "application/json;utf-8");

		HFHttpResp resp = HFHttp.getInstance().httpJsonPost(url, headers, null, params);

		System.out.println("resp::<<<" + resp.getEntity());

	}

}
